package sjtu.ist.warehouse4ws.action;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;

import sjtu.ist.warehouse4ws.model.Domainmodel;
import sjtu.ist.warehouse4ws.model.Modelgroup;
import sjtu.ist.warehouse4ws.model.Process;
import sjtu.ist.warehouse4ws.model.Resource;
import sjtu.ist.warehouse4ws.model.Role;
import sjtu.ist.warehouse4ws.model.Epcunits;
import sjtu.ist.warehouse4ws.model.Functionpoint;
import sjtu.ist.warehouse4ws.model.VisionInfoPojo;
import sjtu.ist.warehouse4ws.service.ModelgroupServiceImpl;
import sjtu.ist.warehouse4ws.service.RoleService;
import sjtu.ist.warehouse4ws.service.DomainModelService;
import sjtu.ist.warehouse4ws.service.ProcessService;
import sjtu.ist.warehouse4ws.service.FunctionpointService;
import sjtu.ist.warehouse4ws.service.EpcunitsService;
import com.opensymphony.xwork2.ActionSupport;
import sjtu.ist.warehouse4ws.jcr.RepositoryMoudl;
import sjtu.ist.warehouse4ws.service.ModelgroupService;
/**
 * by Steve Jin
 */

public class ShowVersionInfoAction extends ActionSupport{


	private RoleService roleService;
	private DomainModelService domainModelService; 
	private ProcessService processService; 
	private FunctionpointService functionpointService;
	private EpcunitsService epcunitsService;
	
	public void setRepositoryMoudlService(RepositoryMoudl repositoryMoudlService){
		this.repositoryMoudlService = repositoryMoudlService;
	}
	RepositoryMoudl repositoryMoudlService;
	ArrayList<VisionInfoPojo> list=new ArrayList<VisionInfoPojo> ();
	

	ModelgroupServiceImpl modelgroupService;
	
	public void setModelgroupService(ModelgroupServiceImpl modelgroupService) {
		this.modelgroupService = modelgroupService;
	}
	private List<Role> roleList=new ArrayList<Role>();
	private List<Domainmodel> domainModelList = new ArrayList<Domainmodel> ();
	private List<Process> processList = new ArrayList<Process> ();
	private List<Functionpoint> functionpointList = new ArrayList<Functionpoint> ();
	private List<Epcunits> epcunitsList = new ArrayList<Epcunits> ();
	
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	public void setDomainModelService(DomainModelService domainModelService) {
		this.domainModelService = domainModelService;
	}
	
	public void setProcessService(ProcessService processService) {
		this.processService = processService;
	}
	public void setFunctionpointService(FunctionpointService functionpointService) {
		this.functionpointService = functionpointService;
	}
	public void setEpcunitsService(EpcunitsService epcunitsService) {
		this.epcunitsService = epcunitsService;
	}
	
	
	public String showRoleVersionInfo(){
		Subject currentUser = SecurityUtils.getSubject();
		String str=currentUser.getSession().getAttribute("companyId").toString();	
		HttpServletRequest request = ServletActionContext.getRequest ();
		String roleId = request.getParameter("cid");
		int type =1;
		roleList = roleService.getRoleListById(roleId);		
		String groupId = roleList.get(0).getGroupId();     //获取groupId;
		int i = Integer.valueOf(groupId).intValue();
		String groupName = modelgroupService.get(i).getGroupName();
		list=repositoryMoudlService.showVersion(str, groupName, type, roleId);
//		System.out.println("--------------------GroupId:"+groupId);
//		System.out.println("------------------companyId:"+str);
//		System.out.println("----------------------------roleId:"+roleId);
		return SUCCESS;
	}
	
	public String showDomainModelVersionInfo(){
		Subject currentUser = SecurityUtils.getSubject();
		String str=currentUser.getSession().getAttribute("companyId").toString();	
		HttpServletRequest request = ServletActionContext.getRequest ();
		String domainId = request.getParameter("cid");
		int type =2;
		domainModelList = domainModelService.getDomainmodelListById(domainId);		
		String groupId = domainModelList.get(0).getGroupId();     //获取groupId;
	
		int i = Integer.valueOf(groupId).intValue();
		String groupName = modelgroupService.get(i).getGroupName();
		//System.out.print(i);
		list=repositoryMoudlService.showVersion(str, groupName, type, domainId);		
		return SUCCESS;
	}
	
	public String showProcessVersionInfo(){
		Subject currentUser = SecurityUtils.getSubject();
		String str=currentUser.getSession().getAttribute("companyId").toString();	
		HttpServletRequest request = ServletActionContext.getRequest ();
		String processId = request.getParameter("cid");
		int type =3;
		processList = processService.getProcessListById(processId);		
		String groupId = processList.get(0).getGroupId();     //获取groupId;
		int i = Integer.valueOf(groupId).intValue();
		String groupName = modelgroupService.get(i).getGroupName();
		list=repositoryMoudlService.showVersion(str, groupName, type, processId);
		return SUCCESS;
	}
	
	public String showFunctionpointVersionInfo(){
		Subject currentUser = SecurityUtils.getSubject();
		String str=currentUser.getSession().getAttribute("companyId").toString();	
		HttpServletRequest request = ServletActionContext.getRequest ();
		String functionpointId = request.getParameter("cid");
		int type =4;
		functionpointList = functionpointService.getFunctionPoint(functionpointId);
		String groupId = functionpointList.get(0).getGroupId();     //获取groupId;
		int i = Integer.valueOf(groupId).intValue();
		String groupName = modelgroupService.get(i).getGroupName();
		list=repositoryMoudlService.showVersion(str, groupName, type, functionpointId);
		return SUCCESS;
	}
	
	public String showEpcunitsVersionInfo(){
		Subject currentUser = SecurityUtils.getSubject();
		String str=currentUser.getSession().getAttribute("companyId").toString();	
		HttpServletRequest request = ServletActionContext.getRequest ();
		String epcunitsId = request.getParameter("text");
		int type =5;
		epcunitsList = epcunitsService.getEpcunitsListById(epcunitsId);
		String groupId = epcunitsList.get(0).getGroupId();     //获取groupId;
		int i = Integer.valueOf(groupId).intValue();
		String groupName = modelgroupService.get(i).getGroupName();
		list=repositoryMoudlService.showVersion(str, groupName, type, epcunitsId);
		return SUCCESS;
	}
	
	@JSON(name = "totalVersionInfoSize")	
	public int getTotalSize() {
		return this.list.size();
	}

	@JSON(name ="versionInfo")	
	public ArrayList<VisionInfoPojo> getList() {
		return list;
	}

}